package com.css.fxfzypg.util;

import com.css.fxfzypg.config.rest.HttpsRestTemplate;
import com.css.util.SM3Util;
import org.slw.common.helper.PropsHelper;
import org.slw.rest.client.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import java.util.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author fanjidong
 * @Title: RestTemplateUtil
 * @date 2021/3/1112:55
 */
@Component
public class RestTemplateUtil {

    @Autowired
    private HttpsRestTemplate httpsRestTemplate;

    public ResponseEntity<String> sendHttpRequest(String url, Map<String, Object> paramMap){
        HttpHeaders requestHeaders = new HttpHeaders();

        //读取配置文件
        Properties CONFIG_PROP = PropsHelper.loadProps("slw-config.properties");
        if("true".equals(CONFIG_PROP.getProperty("openGateway"))){
            //生成网关调用的token
            String token = TokenUtil.getToken();
            requestHeaders.add(CONFIG_PROP.getProperty("getewayToken"), token);
        }

        // 请求头里增加 sysId,sendTime,slw.rest.token
        String sendTimeStr = new Date().getTime() + "";
        requestHeaders.add("sysId", CONFIG_PROP.getProperty("app.system.id"));
        requestHeaders.add("sendTime", sendTimeStr);
        String first = "";
        Set<String> keySet = paramMap.keySet();
        Object[] keys = keySet.toArray();
        Arrays.sort(keys);
        for(Object key:keys){
            first = PlatformObjectUtils.isEmpty(first) ? key + "=" + paramMap.get(key + "") : first + "&" + key + "=" + paramMap.get(key + "");
        }
        String third = "sendTime=" + sendTimeStr;
        if(PlatformObjectUtils.isNotEmpty(first)){
            third = "&" + third;
        }
        String password = CONFIG_PROP.getProperty("app.service.secretkey");
        String fourth = "&accessSecret=" + SM3Util.SM3Encode(PlatformMd5Utils.MD5Encode(password));
        String tokenInfo = PlatformMd5Utils.MD5Encode(first + third + fourth);
        requestHeaders.add("slw.rest.token", tokenInfo.toUpperCase());

        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);

        //String requestUrl =  CONFIG_PROP.getProperty("baseRestUrl") + url;
        String requestUrl =  url;

        ResponseEntity<String> jsonStr;
        try {
            System.out.println("requestUrl:" + requestUrl);
            System.out.println("paramMap:" + paramMap);
            jsonStr = httpsRestTemplate.httpsRestTemplate().exchange(requestUrl, HttpMethod.GET, requestEntity, String.class, paramMap);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("通过远程方式获取base数据失败");
        }
        return  jsonStr;
    }

    public ResponseEntity<String> sendPostHttpRequest(String url, Map<String, Object> paramMap){
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        //读取配置文件
        Properties CONFIG_PROP = PropsHelper.loadProps("slw-config.properties");

        if("true".equals(CONFIG_PROP.getProperty("openGateway"))) {
            //生成网关调用的token
            String token = TokenUtil.getToken();
            requestHeaders.add(CONFIG_PROP.getProperty("getewayToken"), token);
        }

        // 请求头里增加 sysId,sendTime,slw.rest.token
        String sendTimeStr = new Date().getTime() + "";
        requestHeaders.add("sysId", CONFIG_PROP.getProperty("app.system.id"));
        requestHeaders.add("sendTime", sendTimeStr);
        String second = "bodyParam=" + JSON.toJSONString(paramMap);
        String third = "sendTime=" + sendTimeStr;
        if(PlatformObjectUtils.isNotEmpty(second)){
            third = "&" + third;
        }
        String password = CONFIG_PROP.getProperty("app.service.secretkey");
        String fourth = "&accessSecret=" + SM3Util.SM3Encode(PlatformMd5Utils.MD5Encode(password));
        String tokenInfo = PlatformMd5Utils.MD5Encode(second + third + fourth);
        requestHeaders.add("slw.rest.token", tokenInfo.toUpperCase());

        HttpEntity<String> requestEntity = new HttpEntity<String>(JSONObject.toJSONString(paramMap), requestHeaders);

        //String requestUrl =  CONFIG_PROP.getProperty("baseRestUrl") + url;
        String requestUrl =   url;
        ResponseEntity<String> jsonStr;
        try {
            System.out.println("requestUrl:" + requestUrl);
            System.out.println("paramMap:" + paramMap);
            jsonStr = httpsRestTemplate.httpsRestTemplate().exchange(requestUrl, HttpMethod.POST, requestEntity, String.class, paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("通过远程方式提交base数据失败");
        }
        return  jsonStr;
    }
}
